# ==============================
# _*_ coding:utf-8 _*_
# _*_ by:zizle     _*_
# _*_ at:20230415  _*_
# ==============================

from fastapi.responses import JSONResponse


class APIResponse:
    SUCCESS_CODE = 2000
    NOT_CONTENT = 2040
    SERVER_ERROR = 5000
    VALIDATE_ERROR = 4220
    LOGIN_EXPIRE_ERROR = 4010
    USER_INACTIVATE_ERROR = 4020
    REJECT_ERROR = 4030

    @classmethod
    def success(cls, data=None, msg='请求成功!'):
        if not data:
            data = {}
        return JSONResponse(
            content={'success': True, 'message': msg, 'code': cls.SUCCESS_CODE, 'data': data},
            status_code=200
        )

    @classmethod
    def reject(cls, data=None, msg='服务器拒绝了您的请求!'):
        if not data:
            data = {}
        return JSONResponse(
            content={'success': True, 'message': msg, 'code': cls.REJECT_ERROR, 'data': data},
            status_code=200
        )

    @classmethod
    def no_content(cls, data=None, msg='请求成功'):
        return JSONResponse(
            content={'success': True, 'message': msg, 'code': cls.NOT_CONTENT, 'data': data},
            status_code=200  # 此处不能使用204，因为实际是有响应数据的
        )

    @classmethod
    def validate_error(cls, detail: str):
        return JSONResponse(
            content={'success': False, 'message': detail, 'code': cls.VALIDATE_ERROR, 'data': {}},
            status_code=200
        )

    @classmethod
    def http_error(cls, detail: str, code: int):
        return JSONResponse(
            content={'success': False, 'message': detail, 'code': cls.SERVER_ERROR, 'data': {}},
            status_code=200
        )

    @classmethod
    def server_error(cls, detail=None):
        if not detail:
            detail = '服务器错误了...'
        return JSONResponse(
            content={'success': False, 'message': detail, 'code': cls.SERVER_ERROR, 'data': {}},
            status_code=200
        )

    @classmethod
    def login_expire_error(cls):
        return JSONResponse(
            content={'success': False, 'message': '登录已失效!', 'code': cls.LOGIN_EXPIRE_ERROR, 'data': {}},
            status_code=200
        )

    @classmethod
    def user_inactive_error(cls):
        return JSONResponse(
            content={'success': False, 'message': '用户无效或登录信息错误!', 'code': cls.USER_INACTIVATE_ERROR, 'data': {}},
            status_code=200
        )

